package com.module.shici.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import com.module.shici.R;
import com.module.shici.room.Music;

import java.util.List;

public class YinPinListRvAdapter extends RecyclerView.Adapter<YinPinListRvAdapter.ViewHolder> {

    private List<Music> musicList;
    private int currentPosition;
    private int historyPosition;

    public YinPinListRvAdapter(List<Music> musicList, int currentPosition) {
        this.musicList = musicList;
        this.currentPosition = currentPosition;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.module_shici_rv_yinpin_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.title.setText(musicList.get(position).getChaper());
        holder.time.setText("播放量："+ String.format("%.2f", Math.random()*100)  +"万播放");

        if (holder.getLayoutPosition() == historyPosition){
            holder.img.setImageResource(R.mipmap.module_shici_yinyueliebiao_icon_bf);
        }
        if (holder.getLayoutPosition() == currentPosition){
            holder.img.setImageResource(R.mipmap.module_shici_yinyueliebiao_icon_zt);
        }else {
            holder.img.setImageResource(R.mipmap.module_shici_yinyueliebiao_icon_bf);
        }
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //防抖
                if (QuickClickUtils.isFastClick()) {
                    return;
                }
                if (yinPinListListener != null){
                    yinPinListListener.setYinPinListPosition(position);
                    historyPosition = currentPosition;
                    currentPosition = position;
                    notifyItemChanged(historyPosition);
                    notifyItemChanged(currentPosition);
//                    notifyDataSetChanged();
                }
            }
        });
    }

    public interface YinPinListListener{
        void setYinPinListPosition(int position);
    }

    private YinPinListListener yinPinListListener;

    public void getYinPinListPosition(YinPinListListener yinPinListListener){
        this.yinPinListListener = yinPinListListener;
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout relativeLayout;
        private TextView time,title;
        private ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.rv_yinpin_list_time);
            relativeLayout= itemView.findViewById(R.id.rv_yinpin_list_rl);
            title = itemView.findViewById(R.id.rv_yinpin_list_title);
            img = itemView.findViewById(R.id.rv_yinpin_list_img);
        }
    }
}
